//
//  MSDetailDynamicCell.m
//  SocietySale
//
//  Created by Mike on 16/11/10.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSDetailDynamicCell.h"

@interface MSDetailDynamicCell ()
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UIImageView *roundIamge;
@end

@implementation MSDetailDynamicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        self.contentView.clipsToBounds = YES;
    }
    return self;
}

- (void)initView {
    self.timeLabel = [self creatLabel:CGRectMake(0, 2, 70, 20)];
    self.timeLabel.numberOfLines = 0;
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = [self creatLabel:CGRectMake(80, 0, SCREEN_WIDTH-90, 25)];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = HeitiFont(15);
    
    self.contentLabel = [self creatLabel:CGRectMake(80, self.titleLabel.bottom+3, SCREEN_WIDTH-90, 25)];
    self.contentLabel.numberOfLines = 0;
    self.typeLabel = [self creatLabel:CGRectMake(80, self.contentLabel.bottom+5, SCREEN_WIDTH-90, 25)];
    self.typeLabel.textColor = kLightGrayColor;
    
    _verticalLineView =[[UIView alloc] initWithFrame:CGRectMake(70, 7.5, 1, 120)];
    _verticalLineView.backgroundColor = kBlueColor;
    [self.contentView addSubview:_verticalLineView];
    
    _roundIamge = [[UIImageView alloc] initWithFrame:CGRectMake(_verticalLineView.centerX-4, _verticalLineView.top, 8, 8)];
    _roundIamge.image = [UIImage imageNamed:@"stateCycle"];
    _roundIamge.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_roundIamge];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

- (void)setModel:(MSProjectDynamicModel *)model {
    _model = model;
    
    if ([HDUtil isCurrentYear:model.time]) {//今年
        if ([HDUtil isToday:model.time]) {//今天
            self.timeLabel.text = [HDUtil formatter:@"HH:mm" FromeDateStr:model.time];
        }
        else{//非今天
            self.timeLabel.text = [HDUtil formatter:@"MM.dd" FromeDateStr:model.time];
        }
    }
    else{//非今年
        NSString *timeStr = [HDUtil formatter:@"YYYY.MM.dd" FromeDateStr:model.time];
        NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc] initWithString:timeStr];
        NSAttributedString *temp = [[NSAttributedString alloc] initWithString:@"\n"];
        [mutStr insertAttributedString:temp atIndex:5];
        self.timeLabel.attributedText = mutStr;
    }
    CGSize timeSize = [HDUtil sizeWithString:self.timeLabel.text font:HeitiFont(14) size:CGSizeMake(70, CGFLOAT_MAX)];
    if (timeSize.height>20) {
        self.timeLabel.height = 40;
    }
    else{
        self.timeLabel.height = 20;
    }
    self.titleLabel.text = model.title;
    self.contentLabel.text = model.brief;
    
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:model.type ];
    if ([model.memberLevel integerValue] >0) {
        NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
        memberAttachment.image = [UIImage imageNamed:@"diamond"];
        NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
        
        for (int i = 0; i<[model.memberLevel integerValue]; i++) {
            [memberAttstr insertAttributedString:memberAttachmentStr atIndex:model.type.length+i];
        }
    }
    self.typeLabel.attributedText = memberAttstr;
    
    CGSize introductionSize = [HDUtil sizeWithString:model.brief font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-90, 100)];
    if (introductionSize.height>20) {
        self.contentLabel.height = 40;
        _verticalLineView.height = 115;
    }
    else{
        self.contentLabel.height = 20;
        _verticalLineView.height = 90;
    }
    self.typeLabel.top = self.contentLabel.bottom+3;
}

+ (CGFloat)cellHeightWithModel:(MSProjectDynamicModel *)model{
    CGSize introductionSize = [HDUtil sizeWithString:model.brief font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-90, 100)];
    if (introductionSize.height>20) {
        return 115;
    }
    else{
        return 90;
    }
}
@end
